use mlibs::{buf::DMABuffer, periph::{adc::{adc_global_init, ADC1}, config_adc, config_dma, config_gpio}};
use mlibs::periph::rcc::RCC;
use mlibs::periph::dma::DMA2;
use mlibs::periph::gpio::PA;

#[config_adc(ADC1_CH4_CH3)]
struct ADCC;

#[config_dma(DMA2_STREAM0_CH0_SIZE16)]
struct ADCD;

#[config_gpio(IN3_PA3_ANALOG,IN4_PA4_ANALOG)]
struct ADCPin;

static mut ADC_BUF: DMABuffer<u16, 2> = DMABuffer::default();

pub fn init() {
    adc_global_init();
    ADCPin.init();
    ADCC.init();
    ADCD.init_rcc();
    unsafe {
        ADCD.read_with_circ(ADC1.dr.as_ptr() as u32, ADC_BUF.as_mut());
    }
    ADCC.start();
}

pub fn in3() -> u16 {
    unsafe {
        ADC_BUF.as_ref().as_ptr().read_volatile()
    }
}

pub fn in4() -> u16 {
    unsafe {
        ADC_BUF.as_ref().as_ptr().add(1).read_volatile()
    }
}
